package com.cw.jeeyt.service.impl.system;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cw.jeeyt.common.utils.StringUtils;
import com.cw.jeeyt.service.bo.security.SecurityUserBo;
import com.cw.jeeyt.service.bo.system.SystemAreaBo;
import com.cw.jeeyt.core.util.SessionUtil;
import com.cw.jeeyt.dao.intf.system.SystemAreaDao;
import com.cw.jeeyt.dao.model.system.SystemAreaDo;
import com.cw.jeeyt.service.converter.system.SystemAreaConverter;
import com.cw.jeeyt.service.intf.system.SystemAreaService;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.common.utils.SequenceTool;

/**
 * 区域信息业务实现类
 *
 * @author chenw
* @version 1.0.0
 */
@Service
public class SystemAreaServiceImpl implements SystemAreaService {

    private static final Logger logger = LoggerFactory.getLogger(SystemAreaServiceImpl.class);
    @Autowired
    private SystemAreaDao systemAreaDao;

    /**
     * 新增SystemArea
     *
     * @param systemAreaBo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertSystemArea(SystemAreaBo systemAreaBo) throws Exception {

        if (systemAreaBo == null) {
            logger.error("区域信息为空");
            return;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        SystemAreaDo systemArea = SystemAreaConverter.bo2Do(systemAreaBo);
        systemArea.setId(SequenceTool.nextId());
        systemArea.setCreateBy(userVo.getId());
        systemArea.setCreateDate(new Date());
        systemArea.setUpdateBy(userVo.getId());
        systemArea.setUpdateDate(new Date());
        systemAreaDao.insertSystemArea(systemArea);
    }

    /**
     * 更新SystemArea信息
     *
     * @param systemAreaBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateSystemArea(SystemAreaBo systemAreaBo) throws Exception {

        if (systemAreaBo == null) {
            logger.error("区域信息为空");
            return;
        }
        SecurityUserBo userVo = SessionUtil.getSessionUser();
        SystemAreaDo systemArea = SystemAreaConverter.bo2Do(systemAreaBo);
        systemArea.setUpdateBy(userVo.getId());
        systemArea.setUpdateDate(new Date());
        systemAreaDao.updateSystemArea(systemArea);
    }

    /**
     * 删除SystemArea
     *
     * @param systemAreaBo
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteSystemArea(SystemAreaBo systemAreaBo) throws Exception {
        if (systemAreaBo == null) {
            logger.error("区域信息为空");
            return;
        }
        systemAreaDao.deleteSystemArea(SystemAreaConverter.bo2Do(systemAreaBo));

    }

    /**
     * 分页查询SystemArea
     *
     * @param systemAreaBo
     * @param startNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public PageInfo<SystemAreaBo> queryByPage(SystemAreaBo systemAreaBo, Integer startNum, Integer pageSize)
    throws Exception {
        PageInfo<SystemAreaDo> pageInfo = systemAreaDao.queryByPage(SystemAreaConverter.bo2Do(systemAreaBo), startNum,
            pageSize);
        return new PageInfo<>(pageInfo, SystemAreaConverter.doList2BoList(pageInfo.getList()));
    }

    /**
     * 根据城市id加载区域信息
     *
     * @param cityId 城市id
     * @return
     * @throws Exception
     */
    @Override
    public List<SystemAreaBo> loadSystemAreaByCityId(String cityId) throws Exception {

        if (StringUtils.isEmpty(cityId)) {
            logger.error("--主键id为空--");
            return null;
        }
        return SystemAreaConverter.doList2BoList(systemAreaDao.loadSystemAreaByCityId(cityId));
    }

    /**
     * 根据主键查询SystemArea信息
     *
     * @param id
     * @return
     */
    @Override
    public SystemAreaBo getSystemAreaById(String id) throws Exception {
        return SystemAreaConverter.do2Bo(systemAreaDao.getSystemAreaById(id));
    }
}
